//
//  AdvisoryService.m
//  homeland
//
//  Created by SunShine on 13-10-24.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "AdvisoryService.h"
#import "UserInfoService.h"

@implementation AdvisoryService

-(void)postAdvisoryWithTitle:(NSString *)title Name:(NSString *)name Phone:(NSString *)phone Content:(NSString *)content DataItem:(CommonMineQueryItem *)item
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (title) {
        [params setValue:title forKey:@"title"];
    }
    [params setValue:phone forKey:@"phone"];
    [params setValue:name forKey:@"name"];
    [params setValue:content forKey:@"content"];
    if (item.Id && item.Id != (id)[NSNull null]) {
        [params setValue:item.Id forKey:@"id"];
    }
    [params setValue:item.title forKey:@"unitName"];
//    [params setValue:[UserInfoService getUserInfoService].userInfo.userName forKey:@"userName"];
    if ([item.title hasSuffix:@"党代表工作室"]) {
        [params setValue:@"1" forKey:@"policyType"];
    } else if ([item.title hasSuffix:@"人大代表工作室"]) {
        [params setValue:@"2" forKey:@"policyType"];
    } else if ([item.title hasSuffix:@"政协委员工作室"]) {
        [params setValue:@"3" forKey:@"policyType"];
    }else{
        [params setValue:@"0" forKey:@"policyType"];
    }
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    }
    else
    {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }
    [params setValue:getDeviceIdentifier() forKey:@"imsi_code"];//要加入的唯一标示
    [self doHttpPost:POST_ADVISORY_URL headers:nil path:nil parameters:params];
}

-(void)queryDetailAdvisoryWithID:(NSString *)Id
{
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:QUERY_ADVISORY_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:[NSString stringWithFormat:@"%d",Id.integerValue] params:nil httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        //
        
        NSDictionary *json = [completedOperation responseJSON];
        _detailItem = getObjectFromDic([AdvisoryDetailItem class], json);
//        _detailItem.replyContent = @"服务器网络异常服务器网络异常1服务器网络异常服务器网络异常2服务器网络异常服务器网络异常3服务器网络异常服务器网络异常4服务器网络异常服务器网络异常5服务器网络异常服务器网络异常!";
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
            [self.delegate loadDataFinished];
        }
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        //
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"服务器网络异常"];
        }
    }];
    [currentEngine enqueueOperation:op];
}

-(void)feedbackAdvisoryReplyWithID:(NSString *)Id AndVote:(NSInteger)vote
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSString stringWithFormat:@"%d",Id.integerValue] forKey:@"id"];
    [params setValue:[NSString stringWithFormat:@"%d",vote] forKey:@"voteGrade"];
    [self doHttpPost:FEEDBACK_ADVISORY_URL headers:nil path:[NSString stringWithFormat:@"%d",Id.integerValue] parameters:params];
}

-(void)loadDataWithKey:(NSString *)keyOrNil
{
    _currentPageNo = 0;//刷新或查询的时候将请求页码变为0
    if (self.dataList) {
        self.dataList = nil;
    }
    [self startRequest:keyOrNil];
}

-(void)loadMoreDataWithKey:(NSString *)keyOrNil
{
    [self startRequest:keyOrNil];
}

-(void)startRequest:(NSString *)keyOrNil
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"dealCode"];
    } else {
        [params setValue:@"" forKey:@"dealCode"];
    }
    [params setValue:[UserInfoService getUserInfoService].userInfo.userName forKey:@"userName"];
    [params setValue:_unitName forKey:@"unitName"];
    _dataItemName = @"AdvisoryListItem";
    [super doHttpPost:QUERY_ADVISORY_LIST_URL headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo+1] parameters:params];
}

-(void)doHttpPost:(NSString *)posturl headers:(NSDictionary*)headersOrNil path:(NSString*)path parameters:(NSDictionary *)params
{
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if ([params objectForKey:@"userId"]==nil) {
        [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [currentEngine operationWithPath:path params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        BOOL isSuccess = NO;
        if ([[operation responseString] isEqualToString:@"true"]) {
            isSuccess = YES;
        } else {
            NSDictionary *json = [operation responseJSON];
            self.dealCode = [json objectForKey:@"dealCode"];
            isSuccess = ((NSNumber*)[json objectForKey:@"isSuccess"]).boolValue;
        }
        //执行回调
        if (isSuccess) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
                [self.delegate loadDataFinished];
            }
        } else {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
                [self.delegate loadDataFaild:@"提交失败"];
            }
        }
        
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"服务器网络异常"];
        }
    }];
    [currentEngine enqueueOperation:op];
}
@end
